<template>
  <div class="box">
    <div class="common-layout">
      <el-container>
        <el-aside class="aside" width="420px">
          <el-card shadow="never" style="max-width: 400px">
            <template #header>
              <div class="title">
                <span>配 置 单</span>
              </div>
            </template>
            <Sub />
          </el-card>
        </el-aside>
        <el-container>
          <el-main>
            <div class="search">
              <Search />
            </div>
            <div class="main">
              <ContentCard v-for="(item, index) in assembly_data" :key="index" :data="item" :index="index" />
            </div>
            <div class="page">
              <el-pagination @change="changePage" v-model:current-page="page" background :page-size="8" :total="total"
                layout="prev, pager, next" />
            </div>
          </el-main>
        </el-container>
        <el-aside class="aside" width="250px">
          <el-card shadow="never">
            <template #header>
              <div class="title">
                <span>对 比 栏</span>
              </div>
            </template>
            <div class="compare">
              <Compare />
            </div>
          </el-card>
        </el-aside>
      </el-container>
    </div>
  </div>


</template>

<script setup lang="ts">
import { onMounted, ref, watch, } from 'vue';
import Sub from './components/Sub.vue';
import ContentCard from "./components/Content.vue"
import Search from './components/Search.vue';
import Compare from './components/Compare.vue';
import { getCPUData, getGPUData, getMainboardData, getMemoryData, getSSDData, getHDDData } from '@/server/assembly/index.ts'
import { useTabStore } from '@/store/useTabStore.ts'



const tabStore = useTabStore()

//
const page = ref(1)

//
const total = ref(0)

const assembly_data = ref([])





onMounted(() => {
  getAssemblyData(tabStore.assembly_type, page.value, 8, null)
})

const changePage = async (val: number) => {
  page.value = val
  getAssemblyData(tabStore.assembly_type, page.value, 8, tabStore.searchKey)

}


//监听tabStore.assembly_type的变化
watch(() => tabStore.assembly_type, (newVal) => {
  page.value = 1
  getAssemblyData(newVal, page.value, 8, null)
})

//监听tabStore.searchKey的变化
watch(() => tabStore.searchKey, () => {



  getAssemblyData(tabStore.assembly_type, page.value, 8, tabStore.searchKey)
})


const getAssemblyData = async (type: string, page: number, limit: number, searchKey: any) => {
  try {
    let res;
    if (type === 'CPU') {
      res = await getCPUData(page, limit, searchKey)
    } else if (type === 'GPU') {
      res = await getGPUData(page, limit, searchKey)
    } else if (type === '主板') {
      res = await getMainboardData(page, limit, searchKey)
    } else if (type === '内存') {
      res = await getMemoryData(page, limit, searchKey)
    } else if (type === '固态硬盘') {
      res = await getSSDData(page, limit, searchKey)
    } else if (type === '硬盘') {
      res = await getHDDData(page, limit, searchKey)
    }
    assembly_data.value = res?.data.result

    total.value = res?.data.total
  } catch (error) {
    console.log(error);
  }

}


</script>

<style scoped lang="scss">
::v-deep .el-card__body {
  padding: 10px;
}
/* 隐藏所有滚动条 */
::-webkit-scrollbar {
    display: none;
}


.title {
  font-weight: bold;
  display: flex;
  justify-content: center;
  align-items: center;
}


.box {
  height: 100vh;
  margin-top: -70px;
  padding-top: 70px;
  box-sizing: border-box;

  .common-layout {
    height: 100%;

    .el-container {
      height: 100%;

      .aside {
        padding: 10px;
        height: 100%;

        .compare {
          display: flex;
          justify-content: center;
          align-items: center;
        }

      }

      .el-main {
        // background-color: #e9eef3;
        padding: 10px;

        .search {
          margin-bottom: 20px;
        }

        .main {
          display: flex;
          justify-content: space-between;
          flex-wrap: wrap;

        }

        .page {
          display: flex;
          justify-content: center;
          margin-top: 20px;
        }
      }
    }
  }
}
</style>